package com.it.tydic.estate.module.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.tydic.estate.common.util.CurrentUtil;
import com.it.tydic.estate.common.util.InstaIdGenerator;
import com.it.tydic.estate.common.util.TimeUtil;
import com.it.tydic.estate.module.mapper.inner.TbRoleMapper;
import com.it.tydic.estate.module.mapper.inner.TbRoleResourceMapper;
import com.it.tydic.estate.module.model.inner.TbRole;
import com.it.tydic.estate.module.model.inner.TbRoleResource;
import com.it.tydic.estate.module.model.inner.TbUser;
import com.it.tydic.estate.module.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.*;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private TbRoleMapper tbRoleMapper;

    @Autowired
    private TbRoleResourceMapper tbRoleResourceMapper;

    @Override
    public int deleteById(String id) {
        return tbRoleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public TbRole findById(String id) {
        return tbRoleMapper.selectByPrimaryKey(id);
    }


    @Override
    public int deleteByIds(List<String> idList) {
        Example example = new Example(TbRole.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("roleId", idList);
        return tbRoleMapper.deleteByExample(example);
    }

    @Override
    public int addOrUpdate(TbRole tbRole) {
        int index=0;
        String userId= CurrentUtil.getCurrentUserId();
        if(StringUtil.isEmpty(tbRole.getRoleId())){
            int count=countRoleNameByName(tbRole.getRoleName());
            if(count > 0){
                return -1;
            }

            InstaIdGenerator idGenerator = new InstaIdGenerator();
            tbRole.setRoleId(idGenerator.nextId(TimeUtil.timeGen())+"");
            tbRole.setCreateTime(new Date());
            tbRole.setCreateUser(userId);
            tbRole.setStatus(0);
            tbRole.setUpdateTime(new Date());
           index= tbRoleMapper.insert(tbRole);
        }else{
            tbRole.setUpdateUser(userId);
            tbRole.setUpdateTime(new Date());
            index=  tbRoleMapper.updateByPrimaryKeySelective(tbRole);
        }
        return index;
    }

    private int countRoleNameByName(String roleName) {
        Example example = new Example(TbRole.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleName", roleName);
        return  tbRoleMapper.selectCountByExample(example);
    }

    @Override
    public Map<String, Object> findAllByPage(Map<String, String> param) {
        Map<String,Object> map = new HashMap<>();
        String pageNum=param.get("pageNum");
        String pageSize=param.get("pageSize");
        if(StringUtil.isNotEmpty(pageNum) && StringUtil.isNotEmpty(pageSize)){
            PageHelper.startPage(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
            Example example = new Example(TbRole.class);
            example.setOrderByClause("UPDATE_TIME DESC");
            Example.Criteria criteria = example.createCriteria();
            String roleName=param.get("roleName");
            if (StringUtil.isNotEmpty(roleName)) {
                criteria.andLike("roleName","%"+roleName+"%");
            }
            List<TbRole> tbRoles=tbRoleMapper.selectByExample(example);
            for(TbRole tbRole : tbRoles){
              if(  roleIsUseById(tbRole.getRoleId())){
                  tbRole.setResourceFlag("已配置");
              }else{
                  tbRole.setResourceFlag("未配置");
              }
            }
            PageInfo pageInfo=new PageInfo(tbRoles);
            map.put("dataList",pageInfo.getList());
            map.put("total",pageInfo.getTotal());
        }else{
            map.put("dataList",new ArrayList<>());
            map.put("total",0);
        }
        return map;
    }





    @Override
    public int updateRole2Resource(List<String> resourceIds, String roleId) {
        Example example = new Example(TbRoleResource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("resourceId",resourceIds).andEqualTo("roleId",roleId);
        tbRoleResourceMapper.deleteByExample(example);
        InstaIdGenerator idGenerator = new InstaIdGenerator();
        int index=0;
        for(String resourceId : resourceIds){
            TbRoleResource tbRoleResource=new TbRoleResource();
            tbRoleResource.setRoleId(roleId);
            tbRoleResource.setResourceId(resourceId);
            tbRoleResource.setRoleResourceId(idGenerator.nextId(TimeUtil.timeGen())+"");
            tbRoleResourceMapper.insertSelective(tbRoleResource);
            index++;
        }

        return  index;

    }

    @Override
    public List<TbRole> findRoleList() {
        return tbRoleMapper.selectAll();
    }

    @Override
    public boolean roleIsUseByList(List<String> idList) {
        Example example = new Example(TbRoleResource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("roleId", idList);
        int count=countRoleByExample(example);
        if(count > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean roleIsUseById(String id) {
        Example example = new Example(TbRoleResource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleId", id);
        int count=countRoleByExample(example);
        if(count > 0){
            return true;
        }
        return false;
    }

    public int countRoleByExample(Example example){
        return tbRoleResourceMapper.selectCountByExample(example);
    }

}
